class HomeController < ApplicationController

  def index
    if authorize
        @listing = Announcement.find_all_announcements
        @course = Course.find_course
        if session[:loginrole] == "Admin"
            @dashboard = "Go to Gradebook"
        end
        @course_material = CourseMaterial.find(:first, :conditions => ["doc_type = 0"])
    end
  end
  
  def authorize 
	unless session[:loggedin] == true
      flash[:notice] = "Please log in" 
	  redirect_to :controller => :login, :action => :index 
	  return false
	end
	return true 
  end

  def add_announcement
	if params[:newAnnouncement] == "" or params[:newAnnouncement] == nil      #check whether announcement has text
		flash[:notice] = "Please enter some text for an announcement."
		redirect_to :controller => :home, :action => :index
        return
	end
	Announcement.create(:content => params[:newAnnouncement])
	redirect_to :controller => :home, :action => :index
  end
  
  def add_logo
        #recs = CourseMaterial.count
      #CourseMaterial.create(:title => params[:title], :document_url => 'some url', :document => params[:uploaded_document])
      @course_material = CourseMaterial.new(params[:course_material])
      @course_material.save
      @course_material.update_attribute(:doc_type, 0)
      #if CourseMaterial.count > 1
        CourseMaterial.delete_old_logo
      #end
      redirect_to :controller => :home, :action => :index, :id => @course_material.id
   end
  
  def Delete_all_announcements
    @entries = Announcement.find(:all)
    if not @entries.nil?
        @entries.each{|announcement|
            announcement.delete
        }
    	flash[:notice] = "All announcements have been deleted."
    end
	redirect_to :controller => :home, :action => :index    
  end
  
  def course_material
    @course_material = CourseMaterial.find(params[:id])
    send_data(@course_material.document,
              :type => @course_material.content_type,
              :disposition => "inline")
  end
  
end
